package com.ml.mall.service.company.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.company.CompanyIntrIndustryDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.company.SysCompanyInterestIndustry;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.company.SysCompanyInterestIndustryMapper;
import com.ml.mall.service.company.SysCompanyInterestIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysCompanyInterestIndustryServiceImpl extends ServiceImpl<SysCompanyInterestIndustryMapper, SysCompanyInterestIndustry> implements SysCompanyInterestIndustryService{

    @Autowired
    private SysCompanyInterestIndustryMapper sysCompanyInterestIndustryMapper;

    @Override
    public Integer insertBatch(Long companyId, CompanyIntrIndustryDto dto) {
        if(null == dto || null == companyId){
            return 0;
        }
        List<Long> interestedIds = dto.getInterestedIdArr();
        Integer updates = 0;
        //插入一般产业
        if(null != interestedIds && interestedIds.size() > 0){
            updates += sysCompanyInterestIndustryMapper.insertBatch(companyId, interestedIds);
        }
        //插入其他产业
        if(!StrUtil.isBlank(dto.getOtherName())){
            SysCompanyInterestIndustry otherIndustry = new SysCompanyInterestIndustry();
            otherIndustry.setIndustryOther(dto.getOtherName());
            otherIndustry.setIndustryId(CommonConsts.INDUSTRY_OTHER);
            otherIndustry.setCompanyId(companyId);
            updates += sysCompanyInterestIndustryMapper.insert(otherIndustry);
        }
        return updates;
    }

    /**
     * 根据公司id删除感兴趣产业信息
     * @param companyId
     * @return
     */
    @Override
    public Integer removeByCompanyId(Long companyId) {
        LambdaUpdateWrapper<SysCompanyInterestIndustry> deleteWrapper = new LambdaUpdateWrapper<>();
        deleteWrapper.eq(SysCompanyInterestIndustry::getCompanyId, companyId);
        return sysCompanyInterestIndustryMapper.delete(deleteWrapper);
    }

    /**
     * 检查公司感兴趣产业是否为空
     * @param intrIndustryDto 感兴趣的产业
     * @param companyType 公司类型
     * @return
     */
    @Override
    public void checkInterestedIndustry(CompanyIntrIndustryDto intrIndustryDto, Integer companyType) {
        //买家公司，缺少感兴趣产业
        if(CommonConsts.ROLE_TYPE_PURCHASE.equals(companyType)){
            //没有选择感兴趣的产业
            if(null == intrIndustryDto){
                throw new BizException(CommonEnum.INTERESTED_INDUSTRY_IS_NULL);
            }
            //没有选择列表中的产业
            if(null == intrIndustryDto.getInterestedIdArr()
                    || CommonConsts.FIXED_NUM_ZERO.equals(intrIndustryDto.getInterestedIdArr().size())){
                //没有选择其他产业
                if(StrUtil.isBlank(intrIndustryDto.getOtherName())){
                    throw new BizException(CommonEnum.INTERESTED_INDUSTRY_IS_NULL);
                }
            }
        }
    }
}
